/**
 *
 * Copyright 2010 TeleNav, Inc. All rights reserved.
 * EntryView.java
 *
 */
package com.telenav.module.entry;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.telenav.app.R;
import com.telenav.mvc.AbstractBaseView;
import com.telenav.ui.CommandEvent;
import com.telenav.ui.AndroidDialog;
import com.telenav.ui.UiEvent;
import com.telenav.ui.AndroidView;

class EntryViewTouch extends AbstractBaseView implements IEntryConstants
{

	private Context context;
	
	private int selectedIndex;
	private ArrayList<View> pageViews;
	private int[] welPictures = { R.drawable.welcome1, R.drawable.welcome2 };
	
    public EntryViewTouch(Context context)
    {
    	this.context = context;
    }

	@Override
	protected AndroidDialog createDialogDelegate(int state) {
		AndroidDialog dialog = null;
        switch (state)
        {
            case STATE_GOTO_HOME:
            {
                break;
            }
            default:
                break;
        }
        
        return dialog;
	}

	@Override
	protected boolean updateDialog(int state, AndroidDialog dialog) {
		
		return false;
	}

	@Override
	protected AndroidView createViewDelegate(int state) {
        switch(state)
        {
            case STATE_INIT:
            {
                return createSplashView(state);
            }
            case STATE_WELCOME:
            	return createWelcomeView(state);
        }
        return null;
	}

	@Override
	protected boolean updateViewDelegate(int state, AndroidView view) {
		return false;
	}
	
	private AndroidView createSplashView(int state)
	{
		LinearLayout fullView = new LinearLayout(context);
        View view1 = View.inflate(context, R.layout.splash,null);
        fullView.addView(view1);
		return new AndroidView(fullView);
	}
	
	private AndroidView createWelcomeView(int state)
	{
		LinearLayout fullView = new LinearLayout(context);
		pageViews = new ArrayList<View>();
		ImageView imageView;
		for (int i = 0; i < welPictures.length; i++) {
			imageView = new ImageView(context);
			imageView.setLayoutParams(new LayoutParams(10, 10));
			imageView.setPadding(10, 0, 10, 0);
			imageView.setBackgroundResource(welPictures[i]);
			imageView.setOnClickListener(new ItemOnClickListener());
			pageViews.add(imageView);
		}

		ViewGroup viewPictures = (ViewGroup) View.inflate(context, R.layout.activitywelcome,
				null);

		ViewPager viewPager = (ViewPager) viewPictures.findViewById(R.id.viewPager);

		viewPager.setAdapter(new NavigationPageAdapter());
		
		fullView.addView(viewPictures);
		
		return new AndroidView(fullView);
	}
	
	class NavigationPageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			selectedIndex = position;
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			selectedIndex = 0;
			((ViewPager) container).removeView(pageViews.get(position));
		}

	}
	
	class ItemOnClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {

			if(selectedIndex == welPictures.length -1)
			{
				startbutton(v);
			}
			return;
		}
	}
	
	public void startbutton(View v) 
	{
		CommandEvent commandEvent = new CommandEvent(CMD_GOTO_HOME);
		UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
		event.setCommandEvent(commandEvent);
		this.handleUiEvent(event);
	}
}


